package cn.kshost.zqyg.manager.helper;

import cn.kshost.zqyg.model.entity.system.SysMenu;

import java.util.ArrayList;
import java.util.List;

public class TreeNodeHelper {
    //构建所有根菜单
    public static List<SysMenu> getTreeNodes(List<SysMenu> sysMenus) {
        List<SysMenu> menusTree = new ArrayList<>();
        for (SysMenu sysMenu : sysMenus) {
            if (sysMenu.getParentId() == 0) {
//                sysMenu.setChildren(new ArrayList<>());
                SysMenu sysMenuTreeNode = buildTreeNode(sysMenu,sysMenus);
                menusTree.add(sysMenuTreeNode);
            }
        }
        return menusTree;
    }
    //构建单个根菜单
    private static SysMenu buildTreeNode(SysMenu sysMenu, List<SysMenu> sysMenus) {
        sysMenu.setChildren(new ArrayList<>());
        for (SysMenu sysMenuChild : sysMenus) {
            if (sysMenu.getId() == sysMenuChild.getParentId()  ) {
                sysMenu.getChildren().add(buildTreeNode(sysMenuChild,sysMenus));
            }
        }
        return sysMenu;
    }
}
